#############
# FUNCTIONS #
#############

# Returns all header files in the current directory, recursing into sub-directories.
function (get_headers DIRECTORY RESULT)
  file (GLOB_RECURSE HEADER_FILES_TMP "${DIRECTORY}/*.h" "${DIRECTORY}/*.hpp" "${DIRECTORY}/*.inl" "${DIRECTORY}/*.pch" "${DIRECTORY}/*.tsd" "${DIRECTORY}/*.tsp")
  set (${RESULT} ${HEADER_FILES_TMP} PARENT_SCOPE)
endfunction ()

# Returns all source files in the current directory, recursing into sub-directories
function (get_sources DIRECTORY RESULT)
  file (GLOB_RECURSE SOURCE_FILES_TMP "${DIRECTORY}/*.c" "${DIRECTORY}/*.cpp")
  set (${RESULT} ${SOURCE_FILES_TMP} PARENT_SCOPE)
endfunction ()

################
# Build script #
################

cmake_minimum_required (VERSION 2.8)
project (jace)

set (jace_VERSION_MAJOR 1)
set (jace_VERSION_MINOR 2)
add_definitions (-DJACE_EXPORTS -DBOOST_AUTO_LINK_NOMANGLE)

set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MP")
IF(CMAKE_COMPILER_IS_GNUCXX)
	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_FROM_MAVEN}")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_FROM_MAVEN}")

if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
  include_directories (include $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/win32 $ENV{BOOST_HOME})
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  include_directories (include $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux $ENV{BOOST_HOME})
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  include_directories (include $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/darwin $ENV{BOOST_HOME})
else (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
  message (FATAL_ERROR "Unexpected system type: ${CMAKE_SYSTEM_NAME}")
endif ()

file (TO_CMAKE_PATH "$ENV{BOOST_HOME}/lib" BOOST_LIBRARY)
link_directories (${BOOST_LIBRARY})

get_headers ("${CMAKE_CURRENT_SOURCE_DIR}/include" HEADER_FILES)
get_sources ("${CMAKE_CURRENT_SOURCE_DIR}/source" SOURCE_FILES)

add_library (jace SHARED ${SOURCE_FILES} ${HEADER_FILES})

file (TO_NATIVE_PATH ${PROJECT_BINARY_DIR} NATIVE_PROJECT_BINARY_DIR)
file (TO_NATIVE_PATH "$ENV{BOOST_HOME}/lib" BOOST_LIBRARY)
if (${CMAKE_GENERATOR} MATCHES "Visual Studio")
  add_custom_command (TARGET jace POST_BUILD COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/post-build.bat \"${CMAKE_CFG_INTDIR}\" \"${BOOST_LIBRARY}\" \"${NATIVE_PROJECT_BINARY_DIR}\")
elseif (${CMAKE_GENERATOR} MATCHES "Unix Makefiles")
  add_custom_command (TARGET jace POST_BUILD COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/post-build.sh \"${BOOST_LIBRARY}\" \"${NATIVE_PROJECT_BINARY_DIR}\")
endif ()
